import { getResultsAPI, getSuggestAPI } from '@/api/search';
import { AppDispatch } from '..';
import { ActionType } from '../reducer/search';

export const getSuggestAction = (q: string) => {
  return async (dispatch: AppDispatch) => {
    const res = await getSuggestAPI(q);
    dispatch({ type: 'search/saveSuggests', payload: res.data.options });
  };
};

export const clearSuggestsAction = (): ActionType => {
  return { type: 'search/saveSuggests', payload: [] };
};

export const setHistoriesAction = (keyword: string): ActionType => {
  return { type: 'search/saveHistories', payload: keyword };
};

export const clearHistoriesAction = (): ActionType => ({
  type: 'search/clearHistories',
});

export const getResultsAction = (q: string, page?: number) => {
  return async (dispatch: AppDispatch) => {
    const res = await getResultsAPI({ q, page });
    dispatch({ type: 'search/saveResults', payload: res.data });
  };
};
export const clearResultsAction = (): ActionType => {
  return {
    type: 'search/clearResults',
  };
};
